<?php
/**
 * 友情连接管理控制器
 */

class FlinkController extends Controller {

	private $model;
	public function __init() {
		$this -> model = K('Flink');
	}

	/**
	 * 显示所有友情连接列表
	 */
	public function index() {
		//获得所有的友情连接总数
		$linkCount = $this -> model -> count();
		//使用分页类
		$page = new Page($linkCount, 14, 10);
		$flinks = $this -> model -> findAllLink($page -> limit());
		$this -> assign('page', $page -> show(1));
		$this -> assign('flinks', $flinks);
		$this -> display();
	}

	/**
	 * 添加友情连接
	 */
	public function addFlink() {
		if (IS_POST) {
			//先判断 是否存在上传文件如果存在那么就上传否则 就不上传
			if ($_FILES['logo']['error'] ==4) {
					Q('post.logo', '');
			} else {
				$upload = new Upload();
				//当前上传的新文件名
				$fileName = $upload->upload('logo');
				$img = new Image();
				$thumbName = $img->thumb($fileName[0]['path'],$fileName[0]['path'],160,28,6);
				Q('post.logo',$thumbName);
			}
			if ($this -> model -> addFlink()) {
				$this -> success('添加成功');
			} else {
				$this -> error($this -> model -> error);
			}

		}
		$this -> display();
	}

	/**
	 * 编辑友情连接
	 */
	public function edit() {
		$fid = Q('get.fid');
		if (IS_POST) {
			if ($_FILES['logo']['error'] ==4) {
					Q('post.logo', '');
			} else {
				$upload = new Upload();
				//当前上传的新文件名
				$fileName = $upload->upload('logo');
				$img = new Image();
				$thumbName = $img->thumb($fileName[0]['path'],$fileName[0]['path'],160,28);
				Q('post.logo',$thumbName);
			}
				if ($this -> model -> editLink()) {
					$this -> success('修改成功');
				} else {
					$this -> error($this -> model -> error);
				}
			
		}else{
			$result = $this -> model -> where("fid={$fid}") -> find();
			$this -> assign('result', $result);
			$this -> display();
		}
	}



	/**
	 * 删除友情连接
	 */
	public function delFlink() {
		$fid = Q('get.fid');
		//获得他的 logo图片地址
		$logoPath = $this -> model -> where("fid={$fid}") -> getField('logo');
		if ($logoPath) {
			unlink(dirname(HDPHP_PATH) . '/' . $logoPath);
		}
		if ($this -> model -> delete("fid={$fid}")) {
			$this->ajax(1);
		} else {
			$this->ajax(0);
		}
	}
	
	
		/**
	 * 将连接设置为显示
	 */	
	 public function isShow(){
	 	$fid = Q('get.fid');
		if($this->model->save(array('fid'=>$fid,'is_show'=>1))){
			$this->success('设置为显示');
		}
	 }
	 /**
	  * 将连接设置为隐藏
	  */
	  public function isHide(){
	  	$fid = Q('get.fid');
		if($this->model->save(array('fid'=>$fid,'is_show'=>0))){
			$this->success('设置为隐藏');
		}
	  }
	

}
